package com.thti.mecool.client.activity;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

import com.thti.mecool.client.base.BaseActivity;
import com.thti.mecool.client.base.Cache;
import com.thti.mecool.client.command.send.plan.UploadSend;
import com.thti.mecool.client.model.biz.TaskBizImpl;
import com.thti.mecool.client.model.po.Task;
import com.thti.mecool.client.protocol.plan.PlanUnfinishedProtocol;
import com.thti.mecool.client.util.TimeUtil;
import com.thti.mecool.client.util.Util;
import com.thti.mecool.client.widget.ListWidget;
import com.thti.mecool.client.widget.Toolbar;

public class TaskdoneDetailActivity extends BaseActivity {
	private Toolbar toolbar;
	private ImageButton cancel, upload;
	private ListWidget lw;
	private List<Task> list;
	private HashMap<Integer, String> contents;
	OnClickListener MyOnClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.mecool_task_done_detail_upload:
				if (list.size() > 0) {
					contents = lw.getOtherAdapter().findContents();
					if (contents.size() == list.size()) {
						for (int i = 0; i < list.size(); i++) {
							if (lw.getOtherAdapter().findContents().get(i) != null) {
								upload(lw.getOtherAdapter().findContents()
										.get(i).toString(),
										String.valueOf(list.get(i).getPlanId()),
										String.valueOf(list.get(i).getTaskId()));
							}
						}
						Util.sendToast(TaskdoneDetailActivity.this, "提交成功！");
						finish();
					} else {
						Util.sendToast(TaskdoneDetailActivity.this, "请填写完整！");
					}
				} else {
					Util.sendToast(TaskdoneDetailActivity.this, "当前没有可提交的任务！");
				}
				break;
			default:
				finish();
				break;
			}
		}
	};

	public void upload(String notes, String planId, String planJobId) {
		PlanUnfinishedProtocol planUnfinishedProtocol = new PlanUnfinishedProtocol();
		planUnfinishedProtocol.setNotes(notes);
		planUnfinishedProtocol.setPlanId(planId);
		planUnfinishedProtocol.setPlanJobId(planJobId);
		planUnfinishedProtocol
				.setUserLoginName(getCurrentUser().getLoginName());
		UploadSend.UnfinishedTask(planUnfinishedProtocol);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		noTitle();
		setContentView(R.layout.mecool_task_done_detail);
		Cache.add(this);
		cancel = (ImageButton) findViewById(R.id.mecool_task_done_detail_cancel);
		upload = (ImageButton) findViewById(R.id.mecool_task_done_detail_upload);
		cancel.setOnClickListener(MyOnClickListener);
		upload.setOnClickListener(MyOnClickListener);

		ArrayList<String> listTag = new ArrayList<String>();
		ArrayList<ArrayList<HashMap<String, String>>> taskList = new ArrayList<ArrayList<HashMap<String, String>>>();
		TaskBizImpl taskBizImpl = new TaskBizImpl(this);
		list = taskBizImpl.findTaskDetail(TimeUtil.dateToString(new Date(), 1),
				String.valueOf(getCurrentUser().getId()));
		for (Task s : list) {
			System.out.println(s.getPlanId());
			listTag.add(s.getProjectName());
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("time", s.getStartTime() + " - " + s.getEndTime());
			map.put("addr", s.getStoreName());
			map.put("name", s.getTaskName());
			map.put("isType", s.getIsType());
			ArrayList<HashMap<String, String>> arrlist = new ArrayList<HashMap<String, String>>();
			arrlist.add(map);
			taskList.add(arrlist);
		}
		lw = new ListWidget(this, taskList, listTag);
	}

	@Override
	protected void onResume() {
		super.onResume();
		toolbar = new Toolbar(this);
		Toolbar.left_btn.setOnClickListener(MyOnClickListener);
		Toolbar.right_btn.setVisibility(8);
		toolbar.setimage(Toolbar.left_btn, R.drawable.mecool_go_back);
		toolbar.setToolbarCentreText("未完成任务");
	}

}
